﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Public;
using BLL;
using DevComponents.DotNetBar;


namespace QLNhanKhau
{
    public partial class frmKhaiSinh :Form
    {
        public frmKhaiSinh()
        {
            InitializeComponent();
        }

        private void frmKhaiSinh_Load(object sender, EventArgs e)
        {
            txtMaNK.Text = DungChung.LoadMaNK().ToString();
            txtMaNK.Enabled = false;
            txtMaKS.Text = DungChung.LoadMaNK().ToString();
            txtMaKS.Enabled = false;

            //load dân tộc
            cmbDanToc.DataSource = DanTocBLL.LoadDanToc();
            cmbDanToc.DisplayMember = "TenDanToc";
            cmbDanToc.ValueMember = "MaDT";
            //load quoc tich
            cmbQuocTich.DataSource = QuocTichBLL.LoadQuocTich();
            cmbQuocTich.DisplayMember = "TenQuocTich";
            cmbQuocTich.ValueMember = "MaQT";
            //load quan hệ chủ hộ
            cmbQuanHeChuHo.DataSource = QuanHeBLL.LoadQuanHe();
            cmbQuanHeChuHo.DisplayMember = "TenQuanHe";
            cmbQuanHeChuHo.ValueMember = "MaQH";
            //load quan hệ khai sinh
            cmbQuanHeKS.DataSource = QuanHeBLL.LoadQuanHe();
            cmbQuanHeKS.DisplayMember = "TenQuanHe";
            cmbQuanHeKS.ValueMember = "MaQH";
            //load sổ hộ khẩu
            //dgvSoHoKhau.DataSource = SoHoKhauBLL.LoadSoHoKhau();
            //dgvSoHoKhau.Columns[0].HeaderText = "Mã SHK";
            //dgvSoHoKhau.Columns[1].HeaderText = "Số nhà";
            //dgvSoHoKhau.Columns[2].HeaderText = "Tên đường";
            //dgvSoHoKhau.Columns[3].HeaderText = "KP/Ấp";
            //dgvSoHoKhau.Columns[4].Visible = false;

            //dgvSoHoKhau.Columns[0].Width = 59;
            //dgvSoHoKhau.Columns[1].Width = 50;
            //dgvSoHoKhau.Columns[2].Width = 156;
            //dgvSoHoKhau.Columns[3].Width = 50;
            groupBox1.Enabled = false;
            groupBox2.Enabled = false;

            cmbNguoiNhan.DataSource = NguoiDungBLL.LoadNguoiCap();
            cmbNguoiNhan.DisplayMember = "HoTen";
            cmbNguoiNhan.ValueMember = "MaND";

            cmbCanBoDangKi.Text = frmDangNhap.user_login.HoTen.ToString();
            //.SelectedIndex = 1;
            //cmbCanBoDangKi.SelectedValue = frmDangNhap.user_login.MaND;
            //cmbCanBoDangKi.SelectedItem = frmDangNhap.user_login.MaND;
            //cmbCanBoDangKi.v = frmDangNhap.user_login.MaND;
            //load chuc vu

            NguoiDungPublic nd = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiNhan.SelectedValue);
            //.Text = nd.ChucVu;
            txtChucVuNguoiNhan.Text = ChucVuBLL.LoadChucVuByMaCV(nd.MaCV).TenChucVu;
            txtChucVuNguoiNhan.Enabled = false;

            dateNgayNhap.Value = DateTime.Parse(DateTime.Now.ToString());

            //load cho phần khai sinh

            cmbNguoiCap.DataSource = NguoiDungBLL.LoadChuTich();
            cmbNguoiCap.DisplayMember = "HoTen";
            cmbNguoiCap.ValueMember = "MaND";

            cmbCanBoHoTich.DataSource = NguoiDungBLL.LoadCanBoHoTich();
            cmbCanBoHoTich.DisplayMember = "HoTen";
            cmbCanBoHoTich.ValueMember = "MaND";

            txtChucVu.Text = ChucVuBLL.LoadChucVuByMaCV(NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCap.SelectedValue).MaCV).TenChucVu;
            txtNoiCap.Text = ThietLapBLL.LoadDiaChi();
            //to màu
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            style.ForeColor = Color.AliceBlue;
            style.BackColor = Color.Red;
            for (int i = dgvNhanKhau.RowCount - 1; i >= 0; i--)
            {
                if ((int)dgvNhanKhau.Rows[i].Cells[12].Value==0 )
                {
                    //MessageBox.Show("geg");
                    dgvNhanKhau.Rows[i].DefaultCellStyle = style;
                }
                //if ((bool)dgvNguoiDung.Rows[i].Cells[5].Value==true)
                //{
                //  dgvNguoiDung.Rows[i].DefaultCellStyle = style;
                //}

            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult result =  MessageBox.Show( "Noi dung", "Tieu de", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result == DialogResult.Yes)
            {
                this.MdiParent = null;
                this.Dispose();
            }
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            if (txtSoSHK.Text == "")
            {
                MessageBox.Show("Nhập số sổ hộ khẩu cần tìm", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            int SoSHK = int.Parse(txtSoSHK.Text);
            SoHoKhauPublic shk = SoHoKhauBLL.LoadSoHoKhauBySoSHK(SoSHK);
            if (shk == null)
            {
                txtDuong.Text = txtAp.Text = txtSoNha.Text = txtChuHo.Text = "";
                dgvNhanKhau.DataSource = null;
                groupBox1.Enabled = false;
                groupBox2.Enabled = false;
                MessageBox.Show("Không tìm thấy!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                //SoHoKhauPublic shk = SoHoKhauBLL.LoadSoHoKhauBySoSHK(SoSHK);
                List<NhanKhauPublic> list_nk = NhanKhauBLL.LoadNhanKhauByMaSHK(shk.MaSHK);
                List<NhanKhauPublic> list_nkc = NhanKhauBLL.LoadNhanKhauByMaSHKCha(shk.MaSHK);
                List<NhanKhauPublic> list_nkm = NhanKhauBLL.LoadNhanKhauByMaSHKMe(shk.MaSHK);

                groupBox1.Enabled = true;
                groupBox2.Enabled = true;
                txtChuHo.Text = list_nk.Find(item => item.MaQH == 1).HoTen;

                //load hotencha me
                if (list_nkc.Count != 0)
                {
                    cmbHoTenMe.BindingContext = new BindingContext();
                    cmbHoTenCha.DataSource = list_nkc; //NhanKhauBLL.LoadNhanKhauBySoSHK(SoSHK);
                    cmbHoTenCha.DisplayMember = "HoTen";
                    cmbHoTenCha.ValueMember = "MaNK";
                    cmbHoTenCha.SelectedIndex = -1;
                }
                else
                {
                    MessageBox.Show("Không thể thêm khai sinh", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (list_nkm.Count != 0)
                {
                    cmbHoTenCha.BindingContext = new BindingContext();
                    cmbHoTenMe.DataSource = list_nkm; //NhanKhauBLL.LoadNhanKhauBySoSHK(SoSHK);
                    cmbHoTenMe.DisplayMember = "HoTen";
                    cmbHoTenMe.ValueMember = "MaNK";
                    cmbHoTenMe.SelectedIndex = -1;
                }
                else
                {
                    MessageBox.Show("Không thể thêm khai sinh", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                //if (cmbHoTenCha.SelectedValue != null)
                //{
                //    txtDanTocCha.Text = DanTocBLL.GetDanTocByMaDT((NhanKhauBLL.LoadNhanKhauByMaNK((int)cmbHoTenCha.SelectedValue).MaDT)).TenDanToc.ToString();
                //    txtQuocTichCha.Text = QuocTichBLL.GetQuocTichByMaQT((NhanKhauBLL.LoadNhanKhauByMaNK((int)cmbHoTenCha.SelectedValue).MaQT)).TenQuocTich.ToString();
                //}
                //    //txtChuHo.Text = list_nk.Where(item => item.QuanHe == "Chủ hộ").FirstOrDefault().HoTen;
                //if (cmbHoTenMe.SelectedValue != null)
                //{
                //    txtDanTocMe.Text = DanTocBLL.GetDanTocByMaDT((NhanKhauBLL.LoadNhanKhauByMaNK((int)cmbHoTenMe.SelectedValue).MaDT)).TenDanToc.ToString();
                //    txtQuocTichMe.Text = QuocTichBLL.GetQuocTichByMaQT((NhanKhauBLL.LoadNhanKhauByMaNK((int)cmbHoTenMe.SelectedValue).MaQT)).TenQuocTich.ToString();
                //}
                dgvNhanKhau.DataSource = list_nk;
                //dgvNhanKhau.Columns[0].HeaderText = "Mã nhân khẩu";
                dgvNhanKhau.Columns[1].HeaderText = "Họ và tên";
                //dgvNhanKhau.Columns[3].HeaderText = "Tên gọi khác";
                dgvNhanKhau.Columns[4].HeaderText = "CMND";
                dgvNhanKhau.Columns[5].HeaderText = "Ngày sinh";
                //dgvNhanKhau.Columns[6].HeaderText = "Nơi sinh";
                dgvNhanKhau.Columns[7].HeaderText = "Giới tính";
                dgvNhanKhau.Columns[8].HeaderText = "Quê quán";
                dgvNhanKhau.Columns[9].HeaderText = "Dân tộc";
                dgvNhanKhau.Columns[10].HeaderText = "Nghề nghiệp";
                dgvNhanKhau.Columns[11].HeaderText = "Quốc tịch";
                //dgvNhanKhau.Columns[12].HeaderText = "Chứng tử";
                dgvNhanKhau.Columns[13].HeaderText = "Tôn giáo";
                dgvNhanKhau.Columns[14].HeaderText = "Trình độ";
                //dgvNhanKhau.Columns[15].HeaderText = "Sổ hộ khẩu";
                dgvNhanKhau.Columns[16].HeaderText = "Quan hệ";
                dgvNhanKhau.Columns[0].Visible = false;
                //dgvNhanKhau.Columns[1].Visible = false;
                dgvNhanKhau.Columns[2].Visible = false;
                dgvNhanKhau.Columns[3].Visible = false;
                //dgvNhanKhau.Columns[4].Visible = false;
                //dgvNhanKhau.Columns[5].Visible = false;
                dgvNhanKhau.Columns[6].Visible = false;
                //dgvNhanKhau.Columns[7].Visible = false;
                //dgvNhanKhau.Columns[8].Visible = false;
                dgvNhanKhau.Columns[9].Visible = false;
                //dgvNhanKhau.Columns[10].Visible = false;
                dgvNhanKhau.Columns[11].Visible = false;
                dgvNhanKhau.Columns[12].Visible = false;
                dgvNhanKhau.Columns[13].Visible = false;
                dgvNhanKhau.Columns[14].Visible = false;
                dgvNhanKhau.Columns[15].Visible = false;
                //dgvNhanKhau.Columns[16].Visible = false;
                //load phần sổ hộ khẩu
                txtDuong.Text = shk.Duong;
                txtAp.Text = shk.KhuPho.ToString();
                txtSoNha.Text = shk.SoNha;
            }
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtSoKhaiSinh.Text == "" || dateNgayCap.Text == "" || txtNguoiDangki.Text == ""  || txtQueQuan.Text == "" || txtNoiSinh.Text == "")
            {
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin", "Thông báo nhập liệu", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                return;
            }
            //them khai sinh
            KhaiSinhPublic ks = new KhaiSinhPublic();
            ks.MaKS=int.Parse(txtMaKS.Text);
            ks.SoKhaiSinh =int.Parse(txtSoKhaiSinh.Text);
            ks.NgayCap = DateTime.Parse(dateNgayCap.Text);
            ks.NoiCap=int.Parse(ThietLapBLL.LoadMaDiaChi());
            ks.NguoiDangki = txtNguoiDangki.Text;
            ks.MaQH = (byte)cmbQuanHeKS.SelectedValue;
            //ks.NguoiCap = txtCanBoHoTich.Text;
            //ks.ChucVu = txtCanBoHoTich.Text;
            ks.NguoiCap = (int)cmbNguoiCap.SelectedValue;
            ks.ChucVu = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCap.SelectedValue).MaCV;
            ks.NoiSinh = txtNoiSinh.Text;
            //ks.HoTenCha = txtHoTenCha.Text;
            ks.MaNKC= (int)cmbHoTenCha.SelectedValue;
            ks.MaNKM = (int)cmbHoTenMe.SelectedValue;
            ks.NgayNhap = DateTime.Parse(dateNgayNhap.Text);
            //ks.CanBoDangKi = (int)cmbCanBoDangKi.SelectedValue;
            ks.CanBoDangKi = frmDangNhap.user_login.MaND;
            ks.NguoiNhan = (int)cmbNguoiNhan.SelectedValue;
            ks.CanBoHoTich = (int)cmbCanBoHoTich.SelectedValue;
            ks.ChucVuNguoiNhan = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiNhan.SelectedValue).MaCV;
            
            KhaiSinhBLL.InsertKhaiSinh(ks);

            //thêm nhân khẩu
            NhanKhauPublic nk = new NhanKhauPublic();
            nk.MaNK = int.Parse(txtMaNK.Text);
            nk.HoTen = txtHoTen.Text;
            
            nk.MaKS =int.Parse(txtMaKS.Text);
            nk.NgaySinh = DateTime.Parse(dateNgaySinh.Text);
            if (rdtGioiTinhNam.Checked)
                nk.GioiTinh = true;
            else
                nk.GioiTinh = false;
            nk.QueQuan = txtQueQuan.Text;
            nk.MaDT = (byte)cmbQuocTich.SelectedValue;
            nk.MaQH = (byte)cmbQuanHeChuHo.SelectedValue;
            nk.NgheNghiep = "";
            nk.MaQT = (byte)cmbQuocTich.SelectedValue;
            //nk.MaKT = null;
            nk.MaTG = 14;
            nk.MaTD = 1;
            nk.CMND = "";
            nk.TenGoiKhac = "";
            nk.MaSHK =SoHoKhauBLL.LoadSoHoKhauBySoSHK(int.Parse(txtSoSHK.Text)).MaSHK;
            
            NhanKhauBLL.InsertNhanKhau(nk);
            //load lai form
            frmKhaiSinh_Load(sender, e);
            btnTim_Click(sender, e);
            groupBox1.Enabled = true;
            groupBox2.Enabled = true;

            //kiem tra khi insert
            if (txtSoKhaiSinh.Text == "" || txtHoTen.Text == "" || dateNgaySinh.Text == "" || txtNoiSinh.Text == ""
                || txtQueQuan.Text == "" || txtNguoiDangki.Text == "" || dateNgayCap.Text == ""
                 || txtNoiCap.Text == "" || txtChucVu.Text == "")
            {
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin", "Thông báo", MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }

        }

       
        private void txtSoKhaiSinh_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtSoKhaiSinh.Text.Length > 7 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void cmbHoTenCha_SelectionChangeCommitted(object sender, EventArgs e)
        {
            //MessageBox.Show("cho' Phuong");
            NhanKhauPublic father = NhanKhauBLL.LoadNhanKhauByMaNK((int)cmbHoTenCha.SelectedValue);

            txtDanTocCha.Text = DanTocBLL.GetDanTocByMaDT(father.MaDT).TenDanToc;
            txtDanTocCha.Enabled = false;
            txtQuocTichCha.Text = QuocTichBLL.GetQuocTichByMaQT(father.MaQT).TenQuocTich;
            txtQuocTichCha.Enabled = false;
        }

        private void cmbHoTenMe_SelectionChangeCommitted(object sender, EventArgs e)
        {
            NhanKhauPublic mother = NhanKhauBLL.LoadNhanKhauByMaNK((int)cmbHoTenMe.SelectedValue);

            txtDanTocMe.Text = DanTocBLL.GetDanTocByMaDT(mother.MaDT).TenDanToc;
            txtDanTocMe.Enabled = false;
            txtQuocTichMe.Text = QuocTichBLL.GetQuocTichByMaQT(mother.MaQT).TenQuocTich;
            txtQuocTichMe.Enabled = false;
        }

        

        private void cmbNguoiNhan_SelectionChangeCommitted(object sender, EventArgs e)
        {
            NguoiDungPublic nd = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiNhan.SelectedValue);
            //.Text = nd.ChucVu;
            txtChucVuNguoiNhan.Text = ChucVuBLL.LoadChucVuByMaCV(nd.MaCV).TenChucVu;
            txtChucVuNguoiNhan.Enabled = false;
        }

        private void txtSoSHK_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtSoSHK.Text.Length > 7 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        //private void txtQuanHeChuHo_KeyPress(object sender, KeyPressEventArgs e)
        //{
        //    if ( txtQuanHeChuHo.Text.Length > 11 && !Char.IsControl(e.KeyChar))
        //    {
        //        e.Handled = true;
        //    }
        //}

        private void txtHoTen_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ( txtHoTen.Text.Length > 29 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtNoiSinh_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ( txtNoiSinh.Text.Length > 49 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtQueQuan_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ( txtQueQuan.Text.Length > 49 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtNguoiDangki_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtNguoiDangki.Text.Length > 29 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        //private void txtQuanHeKS_KeyPress(object sender, KeyPressEventArgs e)
        //{
        //    if (txtQuanHeKS.Text.Length > 11 && !Char.IsControl(e.KeyChar))
        //    {
        //        e.Handled = true;
        //    }
        //}

        //private void txtCanBoHoTich_KeyPress(object sender, KeyPressEventArgs e)
        //{
        //    if (txtCanBoHoTich.Text.Length > 29 && !Char.IsControl(e.KeyChar))
        //    {
        //        e.Handled = true;
        //    }
        //}

        //private void txtNguoiCap_KeyPress(object sender, KeyPressEventArgs e)
        //{
        //    if ( txtNguoiCap.Text.Length > 29 && !Char.IsControl(e.KeyChar))
        //    {
        //        e.Handled = true;
        //    }
        //}

        private void txtChucVu_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ( txtChucVu.Text.Length > 15 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtNoiCap_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ( txtNoiCap.Text.Length > 49 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void cmbNguoiCap_SelectionChangeCommitted(object sender, EventArgs e)
        {
            NguoiDungPublic nd = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCap.SelectedValue);
            //.Text = nd.ChucVu;
            txtChucVu.Text = ChucVuBLL.LoadChucVuByMaCV(nd.MaCV).TenChucVu;
            txtChucVu.Enabled = false;
        }

        //private void txtAp_KeyPress(object sender, KeyPressEventArgs e)
        //{
        //    if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtAp.Text.Length > 2 && !Char.IsControl(e.KeyChar))
        //    {
        //        e.Handled = true;
        //    }
        //}
    }
}
